package com.retail.lottery.colalottery.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.util.DateUtils;
import com.retail.lottery.colalottery.entity.ColaAnswerRecord;
import com.retail.lottery.colalottery.entity.ColaQuestionBank;
import com.retail.lottery.colalottery.enums.IsPassEnum;
import com.retail.lottery.colalottery.mapper.ColaAnswerRecordMapper;
import com.retail.lottery.colalottery.mapper.ColaQuestionBankMapper;
import com.retail.lottery.colalottery.service.QuestionCommonCacheService;
import com.retail.lottery.colalottery.utils.CommonRedisKeyUtils;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class QuestionCommonCacheServiceImpl implements QuestionCommonCacheService {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ColaAnswerRecordMapper colaAnswerRecordMapper;
    @Resource
    private ColaQuestionBankMapper colaQuestionBankMapper;

    /**
     * 查询题目详情
     *
     * @param buCode
     * @param setupId
     * @return
     */
    public ColaQuestionBank queryColaQuestionBank(String buCode, String setupId, String nowDateStr) {
        ColaQuestionBank colaQuestionBank = null;
        // 1.查询缓存数据是否为最新数据
        Date nowDate = new Date();
        if (StringUtils.isNotBlank(nowDateStr)) {
            try {
                nowDate = DateUtil.parseDateTime(nowDateStr);
            } catch (Exception e) {
                nowDate = new Date();
                log.error("查询日期异常", e);
            }
        }
        log.info("缓存日期{}", DateUtils.format(nowDate));

        String redisKey = CommonRedisKeyUtils.getQuestionDetailCacheKey(nowDate, buCode, setupId);
        Object cacheLotteryInfo = redisTemplate.opsForValue().get(redisKey);
        if (ObjectUtil.isEmpty(cacheLotteryInfo)) {
            colaQuestionBank = colaQuestionBankMapper.queryQuestionByTime(nowDate, buCode, setupId);
            if (colaQuestionBank != null) {
                redisTemplate.opsForValue().set(redisKey, JSONUtil.toJsonStr(colaQuestionBank), 3, TimeUnit.MINUTES);

            }
        } else {
            // 直接返回缓存内容
            colaQuestionBank = JSONUtil.toBean(cacheLotteryInfo.toString(), ColaQuestionBank.class);
        }
        return colaQuestionBank;
    }

    @Override
    public ColaQuestionBank queryColaQuestionBankById(String buCode, String setupId, Integer questionId) {
        ColaQuestionBank colaQuestionBank = null;
        // 1.查询缓存数据是否为最新数据
        String redisKey = CommonRedisKeyUtils.getQuestionDetailCacheKeyByQuestionId(questionId, buCode, setupId);
        Object cacheLotteryInfo = redisTemplate.opsForValue().get(redisKey);
        if (ObjectUtil.isEmpty(cacheLotteryInfo)) {
            colaQuestionBank = colaQuestionBankMapper.selectById(questionId);
            if (colaQuestionBank != null) {
                redisTemplate.opsForValue().set(redisKey, JSONUtil.toJsonStr(colaQuestionBank), 3, TimeUnit.MINUTES);
            }
        } else {
            // 直接返回缓存内容
            colaQuestionBank = JSONUtil.toBean(cacheLotteryInfo.toString(), ColaQuestionBank.class);
        }
        return colaQuestionBank;
    }

    /**
     * 查询用户的答题记录，是否通过
     *
     * @param questionId
     * @param openId
     * @return
     */
    @Override
    public boolean queryCustomerAnswerResult(Integer questionId, String openId, String oneid) {
        // 1.查询缓存数据是否为最新数据
        String redisKey = CommonRedisKeyUtils.getCustomerAnswerCacheKey(questionId, openId, oneid);
        Object colaAnswerRecordCacheInfo = redisTemplate.opsForValue().get(redisKey);
        ColaAnswerRecord colaAnswerRecord = null;
        if (ObjectUtil.isEmpty(colaAnswerRecordCacheInfo)) {
            colaAnswerRecord = colaAnswerRecordMapper.queryAnswerResult(questionId, openId, oneid);
            if (colaAnswerRecord != null) {
                try {
                    redisTemplate.opsForValue().set(redisKey, JSONUtil.toJsonStr(colaAnswerRecord), 3, TimeUnit.MINUTES);
                } catch (Exception e) {
                    log.error("每周题目封装异常", e);
                }
                return ObjectUtils.isEmpty(colaAnswerRecord.getIsPass()) || colaAnswerRecord.getIsPass() == IsPassEnum.FAIL.getCode()
                        ? Boolean.FALSE : Boolean.TRUE;
            }
        } else {
            // 直接返回缓存内容
            colaAnswerRecord = JSONUtil.toBean(colaAnswerRecordCacheInfo.toString(), ColaAnswerRecord.class);
            return ObjectUtils.isEmpty(colaAnswerRecord)
                    || ObjectUtils.isEmpty(colaAnswerRecord.getIsPass())
                    || colaAnswerRecord.getIsPass() == IsPassEnum.FAIL.getCode()
                    ? Boolean.FALSE : Boolean.TRUE;
        }
        return Boolean.FALSE;
    }


}
